package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

//预约管理
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    /*Excel文件上传，并解析文件内容保存到数据库*/
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){ //接收传递过来的文件
        try {
            //调用工具类，解析文件中的数据,String[]代表一行中的数据， List<String[]>就包含了文件中所有数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            //为了便于向数据库中t_ordersetting表里面批量存储数据,将list中每一个String[]封装为OrderSetting对象
            List<OrderSetting> orderSettingList=new ArrayList<>();
                //1.遍历list
            for (String[] strings : list) {
                //取出数组中存储的String类型的日期数据，strings[0]
                String strDate = strings[0];
                //因为日期在数据库中的存储格式为Date类型，所以需要将String类型的日期转为Date类型
                    //数组中的日期字符串格式是："2009/1/1" ，这种格式可以直接用new Date（"2009/1/1"）转为Date格式
                Date date = new Date(strDate);
                //取出数组中存储的代表可预约人数的String类型数据
                String number = strings[1];
                //创建OrderSetting对象封装数据
                OrderSetting orderSetting = new OrderSetting(date,Integer.parseInt(number));

                //将封装了数据的orderSetting存入orderSettingList集合
                orderSettingList.add(orderSetting);

                //调用服务批量存储数据
                orderSettingService.add(orderSettingList);

            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map> list= orderSettingService.getOrderSettingByMonth(date );
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting );
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
